package cn.antho.pet.controller;

import cn.antho.basic.util.LoginContext;
import cn.antho.org.domain.Employee;
import cn.antho.org.service.IEmployeeService;
import cn.antho.pet.dto.AcceptDto;
import cn.antho.pet.service.ISearchMasterMsgService;
import cn.antho.pet.domain.SearchMasterMsg;
import cn.antho.pet.query.SearchMasterMsgQuery;
import cn.antho.basic.util.AjaxResult;
import cn.antho.basic.util.PageList;
import cn.antho.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryByPage(query);
    }

    /**
     * 发布寻主消息
     * @param searchMasterMsg
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg) {
        searchMasterMsgService.publish(searchMasterMsg);
        return AjaxResult.me().success();
    }

    /**
     * 查询待处理数据
     * state=1 & shopId=当前登录人id（店铺管理员）或null（平台管理员）
     * @param query
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        // 获取当前登录人的信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee emp = employeeService.findByLogininfoId(logininfo.getId());
        // 设置状态为1-待处理
        query.setShopId(emp.getShopId());
        query.setState(1);
        return searchMasterMsgService.selectSearchMasterMsgByParam(query);
    }

    /**
     * 查询待审核数据
     * state=0
     * @param query
     * @return
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query) {
        query.setState(0);
        return searchMasterMsgService.selectSearchMasterMsgByParam(query);
    }

    /**
     * 查询寻主池数据
     * state=1 & shopId为null
     * @param query
     * @return
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query) {
        query.setState(1);
        return searchMasterMsgService.selectSearchMasterMsgByParam(query);
    }

    /**
     * 查询已完成数据
     * state=3
     * @param query
     * @return
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        // 获取当前登录人数据
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Long shopId = employeeService.findByLogininfoId(logininfo.getId()).getShopId();
        // 判断 当shopId为null是即登录人为平台管理员
        // 当shopId不为null时 即登录人为店铺管理员 需要赋给shopId值
        if (shopId != null) {
            query.setShopId(shopId);
        }
        query.setState(3);
        return searchMasterMsgService.selectSearchMasterMsgByParam(query);
    }

    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto dto) {
        searchMasterMsgService.accept(dto);
        return AjaxResult.me().success();
    }

    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
        searchMasterMsgService.reject(msgId, employee);
        return AjaxResult.me().success();
    }

}
